/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.io.IOException;

/**
 * Service class for the Shape module that provides operations on shapes.
 * Implements the API specified in the project requirements.
 */
public class ShapeService {
    private ShapeParser parser;
    
    public ShapeService() {
        this.parser = new ShapeParser();
    }
    
    /**
     * Parses shape definitions from a file.
     * @param filePath Path to the file containing shape definitions
     * @return List of parsed shapes
     * @throws IOException If there's an error reading the file
     */
    public ArrayList<Shape> parseFile(String filePath) throws IOException {
        return parser.parseFile(filePath);
    }
    
    /**
     * Counts shapes by their type.
     * @param shapes List of shapes to analyze
     * @return HashMap with shape type names as keys and counts as values
     */
    public HashMap<String, Integer> countByType(ArrayList<Shape> shapes) {
        return parser.countByType(shapes);
    }
    
    /**
     * Calculates the total area of all shapes.
     * @param shapes List of shapes to analyze
     * @return Total area of all shapes
     */
    public double totalArea(ArrayList<Shape> shapes) {
        return parser.totalArea(shapes);
    }
    
    /**
     * Displays shapes visually (optional stretch goal).
     * @param shapes List of shapes to display
     */
    public void displayShapes(ArrayList<Shape> shapes) {
        // Implementation would use the BounceBox framework to visualize shapes
        // This is a simplified version that just prints shape info
        System.out.println("Displaying " + shapes.size() + " shapes:");
        for (Shape shape : shapes) {
            String type = shape.getClass().getSimpleName();
            System.out.printf("%s at (%.1f, %.1f)\n", type, shape.getX(), shape.getY());
        }
        
        // For actual visualization, you would:
        // 1. Create a BounceBox instance with appropriate dimensions
        // 2. Add all shapes to it
        // 3. Start the visualization
        /*
        BounceBox box = new BounceBox(800, 600);
        for (Shape shape : shapes) {
            box.addShape(shape);
        }
        box.start();
        */
    }
    
    /**
     * Additional helper method to get shape statistics.
     * @param shapes List of shapes to analyze
     * @return Formatted string with shape statistics
     */
    public String getShapeStatistics(ArrayList<Shape> shapes) {
        HashMap<String, Integer> counts = countByType(shapes);
        double area = totalArea(shapes);
        
        StringBuilder stats = new StringBuilder();
        stats.append("Shape Statistics:\n");
        stats.append(String.format("Total shapes: %d\n", shapes.size()));
        stats.append(String.format("Total area: %.2f\n", area));
        stats.append("Count by type:\n");
        
        for (String type : counts.keySet()) {
            stats.append(String.format("  %s: %d\n", type, counts.get(type)));
        }
        
        return stats.toString();
    }
}